/*
 * CalendarHandler.java
 *
 * Created on October 31, 2006, 3:23 PM
 *
 */

package com.asaga.tools;
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.util.*;
/**
 * Calendar Handler Class
 * @author Michael J. Mixon
 */
public class CalendarHandler {
    private Event_DBImpl event;
    private Location_DBImpl location;
    private SystemClient_DBImpl userProfile;
    private String locationid;
    private HttpServletRequest request;
    private HttpSession session;
    /**
     * Creates a new instance of CalendarHandler
     */
    public CalendarHandler() {
    }
    /**
     *
     * Constructor of the Calendar Handler Object
     * @param request - the request object
     */
    public CalendarHandler(HttpServletRequest request){
        event = new Event_DBImpl();
        location = new Location_DBImpl();
        userProfile = new SystemClient_DBImpl();
        this.request = request;
        this.session = request.getSession();
    }
    /**
     * Get Event Method
     * @return reutrns an event object
     */
    public Event_DBImpl getEvent(){
        return event;
    }
    
    /**
     * Get Location Method
     * @return - returns a location object
     */
    public Location_DBImpl getLocation(){
        return location;
    }
    
    /**
     * Set Event Method
     * @param e -- the event object to be set.
     */
    public void setEvent(Event_DBImpl e){
        event = e;
    }
    
    /**
     * Add Event Method
     * @return returns acknowledgement
     */
    public String addEvent(){
        String success = "";
        userProfile = (SystemClient_DBImpl)request.getSession().getAttribute("userProfile");
        String starthr = request.getParameter("starthr");
        String startmin = request.getParameter("startmin");
        String startmer = request.getParameter("startmer");
        String endhr = request.getParameter("endhr");
        String endmin = request.getParameter("endmin");
        String endmer = request.getParameter("endmer");
        String startTime = starthr + ":" + startmin + ":00" + " " + startmer;
        String endTime = endhr + ":" + endmin + ":00" + " " + endmer;
        locationid = request.getParameter("locationid");
        event.setTitle(request.getParameter("title"));
        event.setStartDate(request.getParameter("startDate"));
        event.setEndDate(request.getParameter("endDate"));
        event.setDescription(request.getParameter("description"));
        event.setStartTime(startTime);
        //event.setWarningMessage("Is this information correct?");
        event.setEndTime(endTime);
        if((locationid.equals("")) || (locationid == null)){
            event.setWarningMessage("Please Select a location" +
                    " or insert location information.");
            return event.getWarningMessage();
        } else if((locationid.equals("notlisted"))){
            location.setTitle(request.getParameter("locationTitle"));
            location.setAddr1(request.getParameter("addr1"));
            location.setAddr2(request.getParameter("addr2"));
            location.setCity(request.getParameter("city"));
            location.setState(request.getParameter("state"));
            location.setZip(Integer.parseInt(request.getParameter("zip")));
            location.setWebsite(request.getParameter("website"));
            location.setContactName(request.getParameter("contactName"));
            location.setContactEmail(request.getParameter("contactEmail"));
            location.addLocation();
            location.setNewId();
        } else{
            location.setId(Integer.parseInt(locationid));
        }
        event.setCreatorId(userProfile.getId());
        event.setLocationId(location.getId());
        event.addEvent();
        success = "success";
        userProfile.setWelcomeMsg("Event Added Successfully.");
        return success;
    }
    
    /**
     * Set Location Method
     * @param l -- the location to be set in the session
     */
    public void setLocation(Location_DBImpl l){
        location = l;
    }
    
    /**
     * Add Event To Calendar Method
     */
    public void addEventToCalendar(){
        event = (Event_DBImpl)session.getAttribute("event");
        userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
        userProfile.addEventToCalendar(event.getId(), event.getCreatorId());
    }
    
    /**
     * Remove Event From Calendar Method
     * 
     * method removes an event from the user's calendar
     */
    public void removeEventFromCalendar(){
            event = (Event_DBImpl)session.getAttribute("event");
            userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
            if(request.getParameter("eventId") != null){
             int id = Integer.parseInt(request.getParameter("eventId"));
             try {
                userProfile.removeEventFromCalendar(id);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        else{
            try {
                userProfile.removeEventFromCalendar(event.getId());
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * Add Comment Method
     * 
     * allows user to comment on event
     */
    public void addComment(){
        event = (Event_DBImpl)session.getAttribute("event");
        userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
        String comment = request.getParameter("comment");
        double rating = 0;
        if(request.getParameter("rating") != null)
            rating = Double.parseDouble(request.getParameter("rating"));
        event.addComment(comment, rating, userProfile.getId());
    }
    /**
     * Set Event By Id Method
     * 
     * set's event by the given id
     */
    public void setEventById() {
        int eventId = Integer.parseInt(request.getParameter("eventId"));
        try{
            event.setEventById(eventId);
        }
        catch(SQLException se){
            //stub
        }
        catch(WrongFormatException wfe){
            //stub
        }
    }
    
    /**
     * Remove Event Method
     * 
     * method removes event from data source
     */
    public void removeEvent(){
        event = (Event_DBImpl)session.getAttribute("event");    
        setEventById();
        userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
        event.removeEvent(event.getId(), userProfile.getId());
    }

    /**
     * Get User Profile Method
     * @return returns the userProfile Object
     */
    public SystemClient_DBImpl getUserProfile() {
        return userProfile;
    }

    /**
     * Set User Profile Method
     * @param userProfile sets this.userProfile to userProfile
     */
    public void setUserProfile(SystemClient_DBImpl userProfile) {
        this.userProfile = userProfile;
    }

    /**
     * Update Event Method
     */
    public void updateEvent() {
        event = (Event_DBImpl)session.getAttribute("event");
        String starthr = request.getParameter("starthr");
        String startmin = request.getParameter("startmin");
        String startmer = request.getParameter("startmer");
        String endhr = request.getParameter("endhr");
        String endmin = request.getParameter("endmin");
        String endmer = request.getParameter("endmer");
        String startTime = starthr + ":" + startmin + ":00" + " " + startmer;
        String endTime = endhr + ":" + endmin + ":00" + " " + endmer;
        //locationid = request.getParameter("locationid");
        event.setTitle(request.getParameter("title"));
        event.setStartDate(request.getParameter("startDate"));
        event.setEndDate(request.getParameter("endDate"));
        event.setDescription(request.getParameter("description"));
        event.setStartTime(startTime);
        //event.setWarningMessage("Is this information correct?");
        event.setEndTime(endTime);
        /*if((locationid.equals("")) || (locationid == null)){
            event.setWarningMessage("Please Select a location" +
                    " or insert location information.");
        } else if((locationid.equals("notlisted"))){
            location.setTitle(request.getParameter("locationTitle"));
            location.setAddr1(request.getParameter("addr1"));
            location.setAddr2(request.getParameter("addr2"));
            location.setCity(request.getParameter("city"));
            location.setState(request.getParameter("state"));
            location.setZip(Integer.parseInt(request.getParameter("zip")));
            location.setWebsite(request.getParameter("website"));
            location.setContactName(request.getParameter("contactName"));
            location.setContactEmail(request.getParameter("contactEmail"));
            location.addLocation();
            location.setNewId();
        } else{
            location.setId(Integer.parseInt(locationid));
        }
        event.setCreatorId(userProfile.getId());
        event.setLocationId(location.getId());*/
        event.updateEvent();
        //session.setAttribute("event", event);
    }

    /**
     * Search Events Method
     * @return --returns list of events
     */
    public ResultSet searchEvents() {
        event = (Event_DBImpl)session.getAttribute("event");
        String searchTitle = request.getParameter("title");
        String searchCity = request.getParameter("city");
        String searchState = request.getParameter("state");
        return event.searchEvent(searchTitle, searchCity, searchState);
    }
}
